package com.gt.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.app.common.MergeDiseaseEnum;
import com.gt.app.domain.EsMergeDiseaseHistory;
import com.gt.app.domain.EsTreatCasesItem;
import com.gt.app.domain.dto.EsMergeDiseaseHistoryDto;
import com.gt.app.domain.vo.TreatCasesItemVo;
import com.gt.app.mapper.EsMergeDiseaseHistoryMapper;
import com.gt.app.service.EsMergeDiseaseHistoryService;
import com.gt.app.service.EsTreatCasesItemService;
import com.gt.app.util.CommonUtils;
import com.gt.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 合并疾病及手术史表 服务实现类
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@Service
public class EsMergeDiseaseHistoryServiceImpl extends ServiceImpl<EsMergeDiseaseHistoryMapper, EsMergeDiseaseHistory> implements EsMergeDiseaseHistoryService {

    @Autowired
    private EsTreatCasesItemService esTreatCasesItemService;

    @Override
    @Transactional
    public void add(EsMergeDiseaseHistoryDto esMergeDiseaseHistoryDto) {
        if (esMergeDiseaseHistoryDto == null){
            return;
        }

        LambdaQueryWrapper<EsMergeDiseaseHistory> eq = new LambdaQueryWrapper<EsMergeDiseaseHistory>()
                .eq(EsMergeDiseaseHistory::getEsCheckId, esMergeDiseaseHistoryDto.getEsCheckId());
        EsMergeDiseaseHistory mergeDiseaseHistory = this.getOne(eq);
        if (mergeDiseaseHistory !=null){
            this.remove(eq);
            LambdaQueryWrapper<EsTreatCasesItem> wrapper = new LambdaQueryWrapper<EsTreatCasesItem>().eq(EsTreatCasesItem::getMergeOrdrowid, mergeDiseaseHistory.getOrdrowid());
            esTreatCasesItemService.remove(wrapper);
        }

        EsMergeDiseaseHistory esMergeDiseaseHistory = new EsMergeDiseaseHistory();
        BeanUtils.copyProperties(esMergeDiseaseHistoryDto, esMergeDiseaseHistory);
        this.save(esMergeDiseaseHistory);

        if (esMergeDiseaseHistoryDto.getDiabetesTreatCases() != null) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getDiabetesTreatCases()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.DIABETES_TREAT_CASES.getName());

                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }

        if (esMergeDiseaseHistoryDto.getIBDTreatCases() != null ) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getIBDTreatCases()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.IBD_TREAT_CASES.getName());
                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }

        if (esMergeDiseaseHistoryDto.getHepatitisTreatCases() != null ) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getHepatitisTreatCases()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.HEPATITIS_TREAT_CASE.getName());
                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }

        if (esMergeDiseaseHistoryDto.getCholelithiasisTreatCases() != null ) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getCholelithiasisTreatCases()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.CHOLELITHISASIS__TREAT_CASE.getName());
                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }

        if (esMergeDiseaseHistoryDto.getConstipationTreatCases() != null ) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getConstipationTreatCases()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.CONSTIPATION_TREAT_CASE.getName());
                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }

        if (esMergeDiseaseHistoryDto.getCholecystectomyOperationIndication() != null ) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getCholecystectomyOperationIndication()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.CHOLECYSTECTOMY_OPERATION_INDICATION.getName());
                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }

        if (esMergeDiseaseHistoryDto.getCholecystectomyOperationComplication() != null) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getCholecystectomyOperationComplication()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.CHOLECYSTECTOMY_OPERATION_COMPLICATION.getName());
                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }

        if (esMergeDiseaseHistoryDto.getAppendixOperationIndication() != null ) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getAppendixOperationIndication()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.APPENDIX_OPERATION_INDICATION.getName());
                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }

        if (esMergeDiseaseHistoryDto.getAppendixOperationComplication() != null ) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getAppendixOperationComplication()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.APPENDIX_OPERATION_COMPLICATION.getName());
                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }

        if (esMergeDiseaseHistoryDto.getInflammationType() != null ) {
            ArrayList<EsTreatCasesItem> esTreatCasesItems = new ArrayList<>();
            for (String diabetesTreatCase : esMergeDiseaseHistoryDto.getInflammationType()) {
                EsTreatCasesItem esTreatCasesItem = new EsTreatCasesItem();
                esTreatCasesItem.setMergeOrdrowid(esMergeDiseaseHistory.getOrdrowid());
                esTreatCasesItem.setItem(diabetesTreatCase);
                esTreatCasesItem.setType(MergeDiseaseEnum.INFLAMMATIOM_TYPE.getName());
                esTreatCasesItems.add(esTreatCasesItem);
            }
            esTreatCasesItemService.saveBatch(esTreatCasesItems);
        }
    }

    @Override
    public EsMergeDiseaseHistoryDto getMergeDiseaseHistory(String checkId) {
        EsMergeDiseaseHistoryDto esMergeDiseaseHistoryDto = new EsMergeDiseaseHistoryDto();
        LambdaQueryWrapper<EsMergeDiseaseHistory> eq = new LambdaQueryWrapper<EsMergeDiseaseHistory>().eq(EsMergeDiseaseHistory::getEsCheckId, checkId);
        EsMergeDiseaseHistory esMergeDiseaseHistory = this.getOne(eq);
        if (esMergeDiseaseHistory == null){
            return null;
        }
        BeanUtils.copyProperties(esMergeDiseaseHistory, esMergeDiseaseHistoryDto);

        LambdaQueryWrapper<EsTreatCasesItem> wapper = new LambdaQueryWrapper<EsTreatCasesItem>().eq(EsTreatCasesItem::getMergeOrdrowid, esMergeDiseaseHistory.getOrdrowid());
        List<EsTreatCasesItem> list = esTreatCasesItemService.list(wapper);

        List<String> diabetesTreatCases = new ArrayList<>();
        List<String> IBDTreatCases = new ArrayList<>();
        List<String> hepatitisTreatCases = new ArrayList<>();
        List<String> cholelithiasisTreatCases = new ArrayList<>();
        List<String> constipationTreatCases = new ArrayList<>();
        List<String> cholecystectomyOperationIndication = new ArrayList<>();
        List<String> cholecystectomyOperationComplication = new ArrayList<>();
        List<String> appendixOperationIndication = new ArrayList<>();
        List<String> appendixOperationComplication = new ArrayList<>();
        List<String> inflammationType = new ArrayList<>();
        for (EsTreatCasesItem esTreatCasesItem : list) {
            if (MergeDiseaseEnum.DIABETES_TREAT_CASES.getName().equals(esTreatCasesItem.getType())) {
                diabetesTreatCases.add(esTreatCasesItem.getItem());
            }

            if (MergeDiseaseEnum.IBD_TREAT_CASES.getName().equals(esTreatCasesItem.getType())) {
                IBDTreatCases.add(esTreatCasesItem.getItem());
            }

            if (MergeDiseaseEnum.HEPATITIS_TREAT_CASE.getName().equals(esTreatCasesItem.getType())) {
                hepatitisTreatCases.add(esTreatCasesItem.getItem());
            }

            if (MergeDiseaseEnum.CHOLELITHISASIS__TREAT_CASE.getName().equals(esTreatCasesItem.getType())) {
                cholelithiasisTreatCases.add(esTreatCasesItem.getItem());
            }

            if (MergeDiseaseEnum.CONSTIPATION_TREAT_CASE.getName().equals(esTreatCasesItem.getType())) {
                constipationTreatCases.add(esTreatCasesItem.getItem());
            }

            if (MergeDiseaseEnum.CHOLECYSTECTOMY_OPERATION_INDICATION.getName().equals(esTreatCasesItem.getType())) {
                cholecystectomyOperationIndication.add(esTreatCasesItem.getItem());
            }

            if (MergeDiseaseEnum.CHOLECYSTECTOMY_OPERATION_COMPLICATION.getName().equals(esTreatCasesItem.getType())) {
                cholecystectomyOperationComplication.add(esTreatCasesItem.getItem());
            }

            if (MergeDiseaseEnum.APPENDIX_OPERATION_INDICATION.getName().equals(esTreatCasesItem.getType())) {
                appendixOperationIndication.add(esTreatCasesItem.getItem());
            }

            if (MergeDiseaseEnum.APPENDIX_OPERATION_COMPLICATION.getName().equals(esTreatCasesItem.getType())) {
                appendixOperationComplication.add(esTreatCasesItem.getItem());
            }

            if (MergeDiseaseEnum.INFLAMMATIOM_TYPE.getName().equals(esTreatCasesItem.getType())) {
                inflammationType.add(esTreatCasesItem.getItem());
            }
        }

        esMergeDiseaseHistoryDto.setDiabetesTreatCases(diabetesTreatCases);
        esMergeDiseaseHistoryDto.setIBDTreatCases(IBDTreatCases);
        esMergeDiseaseHistoryDto.setHepatitisTreatCases(hepatitisTreatCases);
        esMergeDiseaseHistoryDto.setCholelithiasisTreatCases(cholelithiasisTreatCases);
        esMergeDiseaseHistoryDto.setConstipationTreatCases(constipationTreatCases);
        esMergeDiseaseHistoryDto.setCholecystectomyOperationIndication(cholecystectomyOperationIndication);
        esMergeDiseaseHistoryDto.setCholecystectomyOperationComplication(cholecystectomyOperationComplication);
        esMergeDiseaseHistoryDto.setAppendixOperationIndication(appendixOperationIndication);
        esMergeDiseaseHistoryDto.setAppendixOperationComplication(appendixOperationComplication);

        return esMergeDiseaseHistoryDto;
    }

}
